package net.monare.mifss.evn;

import net.monare.mifss.MifssHolder;
import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.mod.PoolDefUiModule;
import net.monare.mifss.admin.ui.win.ConfigureStoragePoolWindow;
import net.monare.mifss.admin.ui.win.ConfigureStorageWindow;
import net.monare.mifss.configurations.pojo.ContentPoolDef;
import net.monare.mifss.configurations.pojo.StorageDef;
import net.monare.mifss.exceptions.StorageServiceException;

import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Notification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


public class AddNewPoolDefinitionListener extends BasicMifssListener implements ClickListener {

	private static final long serialVersionUID = -4760242068530787294L;

	private PoolDefUiModule poolDefUiModule;
	private StorageDef storageDef;
	private ConfigureStorageWindow parent;
    private ConfigureStoragePoolWindow parentWindow;



	public AddNewPoolDefinitionListener(StorageDef storageDef, PoolDefUiModule poolDefUiModule, ConfigureStorageWindow parent, ConfigureStoragePoolWindow parentWindow) {

		this.storageDef = storageDef;
		this.poolDefUiModule = poolDefUiModule;
		this.parent = parent;
        this.parentWindow = parentWindow;

	}

	@Override
	public void buttonClick(ClickEvent event) {
		try {

			ContentPoolDef cpd = poolDefUiModule.getContentPoolDef();

			if (cpd != null) {

				getMifssHolder().getStorageService().createContentPool(storageDef.getName(), cpd);
				parent.resetAllPanes();
				Notification.show(MifssMessages.getMessage("sys.success.pool.created"));
                parentWindow.close();

			} else {
				Notification.show(MifssMessages.getMessage("sys.error.pool.def.not.defined"));
			}

		} catch (StorageServiceException e) {
			Notification.show(MifssMessages.getMessage("sys.error.pool.def.add") + " : " + e.getMessage());
		}

	}

}
